import React from 'react';
import './index.less'
import './form.less';
import HRForm from './form';
import Action from './Action';
import Model from './Model';
import {bindAction, bindModel, getAction, getModel, render} from '../../../index';


export default ({modelName, actionName}) => {

    // 如果没有绑定过  就绑定默认的
    if (!getModel(modelName)) {
        bindModel({
            [modelName]: Model
        });
    }
    if (!getAction(actionName)) {
        bindAction({
            [actionName]: Action
        });
    }

    return render({
        models: [modelName],
        actions: [actionName]
    })(class extends React.Component {
        constructor(props) {
            super(props);
            props.action[actionName].init(modelName, actionName);
        }

        render() {
            const {action} = this.props;
            const model = this.props[modelName];

            let otherProps = {};

            Object.keys(this.props).forEach((key) => {
                if (key !== modelName && key !== 'action' && key !== 'children') {
                    otherProps[key] = this.props[key];
                }
            });

            return (
                <HRForm
                    {...otherProps}
                    config={model.config}
                    formData={model.formData}
                    cols={model.cols}
                    onChange={action[actionName].changeForm}
                    status={model.status}
                />
            );
        }
    })
}
